#!/bin/sh
#
# Recovery init script
#

# Standard busybox init
/bin/mount -t proc proc /proc
/bin/mount -o remount,rw,noatime /
/bin/mount -t sysfs sysfs /sys
/bin/mount -t devtmpfs dev /dev 2>/dev/null
/bin/hostname -F /etc/hostname
/bin/mount -t tmpfs tmpfs /tmp

# Newer kernel versions can load firmware from /lib/firmware by themselves without help
# However that can fail during boot due to our initrd ramdisk not being mounted in time
# Retry any pending requests
(cd /sys/class/firmware
for F in *; do
    if [ -d "$F" ]; then
        echo 1 > "$F/loading"
        cat "/lib/firmware/${F/!//}" > "$F/data"
        echo 0 > "$F/loading"
    fi
done)

# Set up ACT LED to be triggered by mmc0
echo mmc0 > /sys/class/leds/led0/trigger

# Rescue shell available on tty2 and tty3
echo 2 >/sys/module/vt/parameters/cur_default
/sbin/getty -L tty2 0 vt100 &
/sbin/getty -L tty3 0 vt100 &
/sbin/getty -L ttyAMA0 115200 vt100 &

# Enable syslog
/etc/init.d/S01logging start > /dev/null

if grep -q vncinstall /proc/cmdline; then
    # VNC server mode. Mainly useful for making screenshots
    export QWS_DISPLAY="VNC:size=800x600:depth=32:0"
    ifup eth0
fi

# Mouse acceleration
#export QWS_MOUSE_PROTO=:accel=1

# Pi touch screen
if [ -e /sys/devices/platform/rpi_ft5406 ]; then
    export QWS_MOUSE_PROTO="linuxinput:/dev/input/event0:grab=1 intellimouse:/dev/input/mice"
fi

SAFE_MODE=`vcgencmd get_config safe_mode_gpio`

if ( [ ${SAFE_MODE#*=} -eq 1 ] && ! grep -q disablesafemode /proc/cmdline ) || grep -q rescueshell /proc/cmdline; then
    sh
else
    RUN_INSTALLER=
    GPIO_TRIGGER=
    KEYBOARD_NO_TRIGGER=
    FORCE_TRIGGER=
    DEFAULT_LANG=
    DEFAULT_KBD=
    DEFAULT_DISPLAY=
    DEFAULT_PARTITION=

    if grep -q runinstaller /proc/cmdline; then
        RUN_INSTALLER=-runinstaller
    fi
    if grep -q gpiotriggerenable /proc/cmdline; then
        GPIO_TRIGGER=-gpiotriggerenable
    fi
    if grep -q keyboardtriggerdisable /proc/cmdline; then
        KEYBOARD_NO_TRIGGER=-keyboardtriggerdisable
    fi
    if grep -q forcetrigger /proc/cmdline; then
        FORCE_TRIGGER=-forcetrigger
    fi
    for p in `cat /proc/cmdline` ; do
        if [ "${p%%=*}" == "lang" ] ; then
            DEFAULT_LANG="-lang ${p#*=}";
        fi
        if [ "${p%%=*}" == "keyboard" ] ; then
            DEFAULT_KBD="-kbdlayout ${p#*=}";
        fi
        if [ "${p%%=*}" == "display" ] ; then
            DEFAULT_DISPLAY="-dispmode ${p#*=}"
        fi
        if [ "${p%%=*}" == "partition" ] ; then
            DEFAULT_PARTITION="-partition ${p#*=}"
        fi
    done

    /usr/bin/recovery $RUN_INSTALLER $GPIO_TRIGGER $KEYBOARD_NO_TRIGGER $FORCE_TRIGGER $DEFAULT_KBD $DEFAULT_LANG $DEFAULT_DISPLAY $DEFAULT_PARTITION -qws 2>/tmp/debug

fi

# Recovery should have rebooted

echo Recovery application crashed
echo Starting shell
sh
